<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 18:07
 * 话题列表
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Topic extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.topic_title|a.topic_content|uuu.user_username|uuu.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['a.topic_is_show'] = $province;
            }
            if(isset($topic_type)&&$topic_type!="")
            {
                $map['a.topic_type'] = $topic_type;
            }
            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['topic_fb_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['topic_fb_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['topic_fb_time'] = ['< time', $end];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('topic')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.topic_user_id','left')
                ->join('up_topic_type utt','utt.topic_type_id = a.topic_type','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('topic')->alias('a')
                ->where($map)
                ->join('up_u_user uuu','uuu.user_id = a.topic_user_id','left')
                ->join('up_topic_type utt','utt.topic_type_id = a.topic_type','left')
                ->page($NowPage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_phone,utt.topic_type_name')
                ->order('a.topic_fb_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                //统计留言,收藏,点赞数量
                $dz_num = Db::table('up_topic_dz')->where('topic_id',$vo['topic_id'])->count();
                $message_num = Db::table('up_topic_message')->where('topic_id',$vo['topic_id'])->count();
                $like_num = Db::table('up_topic_like')->where('topic_id',$vo['topic_id'])->count();
                $vo['dz_num'] = $dz_num;
                $vo['message_num'] = $message_num;
                $vo['like_num'] = $like_num;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?topic_id')){
            session('topic_id',null);
        }
        $topic_type = Db::table('up_topic_type')->where('topic_type_status',1)->select();
        $this->assign('topic_type',$topic_type);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('topic')->where('topic_id',$task_id)->setField('topic_is_show',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('topic')->where('topic_id',$task_id)->setField('topic_is_show',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        Db::table('up_topic')->where('topic_id',$id)->delete();
        //查询这个话题留言数量
        $count = Db::table('up_topic_message')->where('topic_id','IN',$id)->count();
        if($count>0){
            //查询这个留言ID
            $topic_message = Db::table('up_topic_message')->where('topic_id','IN',$id)->column('topic_message_id');
            //删除留言
            Db::table('up_topic_message')->where('topic_id','IN',$id)->delete();
            //查询这个留言回复的数量
            $count1 = Db::table('up_topic_reply')->where('reply_message_id','IN',$topic_message)->count();
            if($count1>0){
                Db::table('up_topic_reply')->where('reply_message_id','IN',$topic_message)->setField('reply_status',2);
            }
        }
         return json(['code'=>200,'msg'=>'删除成功']);
    }

    /***
     * @return mixed
     * 详情
     */
    public function detail(){
        $map['a.topic_id'] = input('param.topic_id');
        $lists = Db::name('topic')->alias('a')
            ->where($map)
            ->join('up_u_user uuu','uuu.user_id = a.topic_user_id','left')
            ->field('a.*,uuu.user_username,uuu.user_phone')
            ->find();
        $this->assign('res',$lists);
        return $this->fetch();
    }

    public function entry(){

        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.topic_message_content|uuu.user_username|uuu.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['topic_message_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['topic_message_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['topic_message_time'] = ['< time', $end];
            }
            $map['topic_id'] =session('topic_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('topic_message')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.topic_message_user','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('topic_message')->alias('a')
                ->where($map)
                ->join('up_u_user uuu','uuu.user_id = a.topic_message_user','left')
                ->page($NowPage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_phone')
                ->order('a.topic_message_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?topic_id')){
            session('topic_id',input('topic_id'));
        }
        return $this->fetch();
    }
}